package com.pm.controller.ledger;


import com.pm.common.annotation.CurrentUser;
import com.pm.common.exception.CommonException;
import com.pm.entity.po.ledger.DataAnalysisPo;
import com.pm.entity.po.ledger.DataAnalysisTaskPo;
import com.pm.entity.po.security.User;
import com.pm.entity.vo.ledger.DataAnalysisVo;
import com.pm.entity.vo.ledger.LedgerVo;
import com.pm.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.pm.common.Response;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.ledger.Ledger;
import com.pm.service.ledger.LedgerService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

@Api(tags = "日常工作台账")
@UserLoginToken
@RestController
@RequestMapping("/Ledger")
public class LedgerController {
    @Autowired
    LedgerService ledgerService;

    /**
     * 查询全部账号
     * @return
     */
    @ApiOperation("查询日常工作台账")
    @PostMapping("/getAllLedger")
    @ResponseBody
    public Response<PageInfo<Ledger>> getAllAward(Ledger ledger) {
        PageInfo<Ledger> result = ledgerService.getAllLedger(ledger);
        Response<PageInfo<Ledger>> res = new Response<PageInfo<Ledger>>(ExpCodeState.query_data_success, result);
        return res;
    }

    /**
     * 删除
     */
    @ApiOperation("删除日常工作台账")
    @PostMapping("/deleteLedger")
    @ResponseBody
    @MyLog(value = "删除日常工作台账")
    public Response<Integer> deleteLedger(@RequestParam("id") Integer id) {
        boolean res = ledgerService.deleteLedger(id);
        ;
        if (res) {
            return new Response<>(ExpCodeState.remove_data_success);
        } else {
            return new Response<>(ExpCodeState.remove_data_failed);
        }
    }

    @ApiOperation("添加日常工作台账")
    @PostMapping("/addLedger")
    @ResponseBody
    @MyLog(value = "添加日常工作台账")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> addLedger(@RequestBody Ledger ledger, @CurrentUser User user) {
        Response<Integer> response = null;
        Integer result = ledgerService.addLedger(ledger, user);
        if (result > 0) {
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }

    @ApiOperation("修改日常工作台账")
    @PostMapping("/updateLedger")
    @ResponseBody
    @MyLog(value = "修改日常工作台账")
    public Response<Integer> updateLedger(@RequestBody Ledger ledger, @CurrentUser User user) {
        boolean res = ledgerService.updateLedger(ledger, user);
        if (res) {
            return new Response<>(ExpCodeState.update_data_success);
        } else {
            return new Response<>(ExpCodeState.update_data_failed);
        }
    }


    /**
     * 根据id查部门
     * @return
     */
    @ApiOperation("Id查询日常工作台账")
    @PostMapping("/getLedgerId")
    @ResponseBody
    public Response<Ledger> getLedgerId(Integer id) {
        Ledger ledger = ledgerService.getLedgerId(id);
        ledger.setMonth(DateUtil.getMonth(ledger.getOccurrenceTime()));
        Response<Ledger> response = new Response<>(ExpCodeState.query_data_success, ledger);
        return response;
    }

    /**
     * 日常信息台账统计
     * @return
     */
    @ApiOperation("查询日常工作台账")
    @PostMapping("/DataAnalysis")
    @ResponseBody
    public Response<DataAnalysisPo> DataAnalysis(@RequestBody DataAnalysisVo dataAnalysisVo) {
        if (dataAnalysisVo.getYear() == null) {
            dataAnalysisVo.setYear(DateUtil.getYear(new Date()));
        }
        DataAnalysisPo result = ledgerService.DataAnalysis(dataAnalysisVo);
        Response<DataAnalysisPo> res = new Response<DataAnalysisPo>(ExpCodeState.query_data_success, result);
        return res;
    }

    /**
     * 重点工作台账统计
     * @return
     */
    @ApiOperation("重点工作台账统计")
    @PostMapping("/DataAnalysisTask")
    @ResponseBody
    public Response<List<DataAnalysisTaskPo>> DataAnalysisTask(@RequestBody LedgerVo ledgerVo) {
        if (ledgerVo.getYear() == null) {
            ledgerVo.setYear(DateUtil.getYear(new Date()));
        }
        // List<DataAnalysisTaskPo> result = ledgerService.DataAnalysisTask(dataAnalysisTaskPo.getId());
        List<DataAnalysisTaskPo> result = ledgerService.getDataAnalysisTask(ledgerVo);
        Response<List<DataAnalysisTaskPo>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }


}
